package org.linys.action.dict;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.linys.action.BaseAction;
import org.linys.model.dict.Goods;
import org.linys.model.system.Operator;
import org.linys.service.dict.GoodsServ;
import org.linys.util.PageUtil;
import org.linys.util.ResponseMsgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
@SuppressWarnings({  "unchecked" })
@Controller("GoodsAction")
@Scope("prototype")
public class GoodsAction extends BaseAction implements ModelDriven{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Goods goods = new Goods();
	private ResponseMsgUtil responseMsgUtil;
	private PageUtil pageUtil;
	private GoodsServ goodsServ;
	
	


	/**
	 * 查看所有的
	 */
	public void view() {
		JSONObject object = new JSONObject();
		try{
			this.pageUtil.cal(goodsServ.count(goods), goods.getRows(), goods.getPage());
			List<Goods> stations = goodsServ.view(goods, this.pageUtil.getStart(), this.pageUtil.getRows());
			
			JSONArray jsonArray = JSONArray.fromObject(stations);
			object.put("total", this.pageUtil.getTotal());
			object.put("rows", jsonArray);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			this.responseMsgUtil.doResponse(object.toString());
		}
	}
	
	/**
	 * 添加
	 */
	public void add() {
		JSONObject object = new JSONObject();
		try{
			Operator currentOperator=(Operator)request.getSession().getAttribute("operator_session");
			goods.setHandlerID(currentOperator.getOperatorID_());
			goods.setOperatorID(currentOperator.getOperatorID_());
			if (goodsServ.save(goods) > 0) {
				object.put("success", 1);
				object.put("goodsID", goods.getGoodsID());
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			this.responseMsgUtil.doResponse(object.toString());
		}
	}
	
	/**
	 * 修改货物
	 */
	public void update() {
			
		JSONObject object = new JSONObject();
		try{
			
			Operator currentOperator=(Operator)request.getSession().getAttribute("operator_session");
			goods.setOperatorID(currentOperator.getOperatorID_());
			if (goodsServ.update(goods) > 0) {
				object.put("success", 1);
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}finally{
			this.responseMsgUtil.doResponse(object.toString());
		}
	}
	
	/**
	 * 批量删除用户
	 */
	public void deleteByIDs() {
		String[] idsStr = this.goods.getIds().split(",");
		for (String id : idsStr) {
			try {
				goodsServ.deleteByID(new Integer(id));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		JSONObject object = new JSONObject();
		object.put("success", 1);
		this.responseMsgUtil.doResponse(object.toString());
	}
	
	
	/**
	 * 选择货物类型
	 */
	public void choose() {
		try{
			
			this.pageUtil.cal(goodsServ.countChoose(goods), goods.getRows(), goods.getPage());

			List<Goods> goodsList = goodsServ.viewChoose(goods, this.pageUtil.getStart(), this.pageUtil.getRows());
			JSONArray jsonArray = JSONArray.fromObject(goodsList);
			JSONObject object = new JSONObject();
			object.put("total", this.pageUtil.getTotal());
			object.put("rows", jsonArray);
			this.responseMsgUtil.doResponse(object.toString());
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 根据ID取得对应的货物
	 */
	public void getByID() {
		try{
			
			
			Goods resultGoods = goodsServ.getByID(goods);
			JSONObject object = new JSONObject();
			object.put("success", 1);
			if(resultGoods!=null){
				object.put("goodsName", resultGoods.getGoodsName());
			}
			this.responseMsgUtil.doResponse(object.toString());
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	
	public Object getModel() {
		return goods;
	}
	public ResponseMsgUtil getResponseMsgUtil() {
		return responseMsgUtil;
	}
	@Autowired
	public void setResponseMsgUtil(ResponseMsgUtil responseMsgUtil) {
		this.responseMsgUtil = responseMsgUtil;
	}
	public PageUtil getPageUtil() {
		return pageUtil;
	}
	@Autowired
	public void setPageUtil(PageUtil pageUtil) {
		this.pageUtil = pageUtil;
	}
	public GoodsServ getGoodsServ() {
		return goodsServ;
	}
	@Autowired
	public void setGoodsServ(GoodsServ goodsServ) {
		this.goodsServ = goodsServ;
	}
	
}
